//
//  TravelsComListVC.m
//  SelfDrivingTravelling-IOS
//
//  Created by Teplot_01 on 16/9/20.
//  Copyright © 2016年 Teplot_01. All rights reserved.
//

#import "TravelsComListVC.h"
#import "ComListCell.h"
@interface TravelsComListVC ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation TravelsComListVC
{
    UITableView *_tableView;
    
    //请求下来的字典
    NSDictionary *_responseDic;
    //请求下来的数组
    NSArray *_responseArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    self.title = @"评论列表";
    
    [self requsetForCommentList];
    [self initTableViewForComList];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)initTableViewForComList{
    _tableView = [[UITableView alloc]initWithFrame:self.view.frame];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
- (void)requsetForCommentList{
    //参数
    NSDictionary *parameters = @{
                                 @"objID":self.travelsId,
                                 @"commType": @"1",
                                 @"objType": @"1",
                                 //                                 @"originalID":@"2",
                                 @"createrID": [NSString readToNSDefaultsWithKey:kId],
                                 };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer=[AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:@"http://115.28.152.130/tour/v1/comment/list.action" parameters:parameters success:^(NSURLSessionDataTask *operation, id responseObject) {
        _responseDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@>>>>",_responseDic);
        _responseArr = _responseDic[@"list"];
        NSString *error = _responseDic[@"code"];
        if (![_responseDic[@"code"] isEqualToString:@"0000"]) {
            [SVProgressHUD showErrorWithStatus:[NSString errorStringWithCode:error]];
            
        }else{
            //请求成功
            [SVProgressHUD showSuccessWithStatus:@"评论列表请求成功"];
            [_tableView reloadData];
        }
        
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求失败"];
    }];
}
#pragma - mark UITabelViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _responseArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ComListCell *cell = [[NSBundle mainBundle]loadNibNamed:@"ComListCell" owner:nil options:nil][0];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSString *createTimeStr = _responseDic[@"list"][indexPath.row][@"createTime"];
    createTimeStr = [createTimeStr substringToIndex:19];
    cell.time.text = createTimeStr;
    cell.name.text = _responseDic[@"list"][indexPath.row][@"createrName"];
    NSString *iconStr = _responseDic[@"list"][indexPath.row][@"createrPhoto"];
    NSDate *currentDate = [NSDate date];//获取当前时间，日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYYMMddHHmmssSSS"];
    NSString *dateString = [dateFormatter stringFromDate:currentDate];
    iconStr = [NSString stringWithFormat:@"%@%@%@",iconStr, @"?",dateString];
    [cell.icon sd_setImageWithURL:[NSURL URLWithString:iconStr] placeholderImage:[UIImage imageNamed:@"head_portrait"]];
    cell.icon.layer.masksToBounds = YES;
    cell.icon.layer.cornerRadius = 5;
    UILabel *comLabel = [[UILabel alloc]initWithFrame:CGRectMake(18, 55, KScreenWidth-26, 25)];
    comLabel.font = [UIFont systemFontOfSize:13];
    comLabel.text = _responseDic[@"list"][indexPath.row][@"comm"];
    [cell.contentView addSubview:comLabel];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
